package com.chixing.day06.clone;

public class CloneShallow {
    public static void main(String[] args) throws CloneNotSupportedException {
        Car car1 = new Car(1,new Engine(221,"v8"),new Tire(331,"米其林"));
        Car car2 = (Car) car1.clone();
        System.out.println(car1.getEngine()+" "+car1.getTire());
        System.out.println(car2.getEngine()+" "+car2.getTire());
        System.out.println(car1==car2);
    }
}
class Car implements Cloneable{
    public int cid;
    public Engine engine;
    public Tire tire;

    public Car(int cid, Engine engine, Tire tire) {
        this.cid = cid;
        this.engine = engine;
        this.tire = tire;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public Tire getTire() {
        return tire;
    }

    public void setTire(Tire tire) {
        this.tire = tire;
    }

//    @Override
//    public Object clone() throws CloneNotSupportedException {
//        //1.先复制对象
//        Car car = (Car) super.clone();
//        //2.复制引用数据
//        car.setEngine((Engine) car.getEngine().clone());
//        car.setTire((Tire) car.getTire().clone());
//        return car;
//    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Car{" +
                "cid=" + cid +
                ", engine=" + engine +
                ", tire=" + tire +
                '}';
    }
}
class Engine implements Cloneable{
    public int eid;
    public String eName;

//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }

    public Engine(int eid, String eName) {
        this.eid = eid;
        this.eName = eName;
    }

    @Override
    public String toString() {
        return "Engine{" +
                "eid=" + eid +
                ", eName='" + eName + '\'' +
                '}';
    }
}
class Tire implements Cloneable{
    public int tid;
    public String tName;

//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }

    public Tire(int tid, String tName) {
        this.tid = tid;
        this.tName = tName;
    }

    @Override
    public String toString() {
        return "Tire{" +
                "tid=" + tid +
                ", tName='" + tName + '\'' +
                '}';
    }
}


